package com.marktotop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.marktotop.entity.Menu;

import com.marktotop.entity.MenuRole;
import com.marktotop.entity.Role;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.IMenuRoleService;
import com.marktotop.service.IMenuService;
import com.marktotop.service.IRoleService;
import com.marktotop.vo.MenusVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * PermissionController 权限组控制
 *      权限组实现:
 *          * 查询所有菜单
 *          * 根据角色id查询菜单id
 *          * 更新角色菜单
 *
 *
 * @author WXLD
 * @Data 2021/6/21
 * @Since 1.0
 */
@RestController
@RequestMapping("/system/basic/permiss")
@Api(tags = "系统角色接口")
public class PermissionController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IMenuRoleService menuRoleService;

    /**
     * 获取所有角色
     *
     * @return
     */
    @ApiOperation(value = "获取所有的角色")
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public ResultBean getAllRoles(){
        List<Role> roles = roleService.list();
        if (null == roles){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().data("roles",roles);
    }

    /**
     * 添加新角色
     *
     * @param role
     * @return
     */
    @ApiOperation(value = "添加角色")
    @RequestMapping(value = "/role",method = RequestMethod.POST)
    public ResultBean addNewRoles(@RequestBody Role role){
        if (!role.getName().startsWith("ROLE_")){
            role.setName("ROLE_"+role.getName());
        }
        if (roleService.save(role)){
            return ResultBean.ok().message("添加角色操作成功!");
        }
        return ResultBean.error().message("添加角色操作失败，请重试!");
    }

    /**
     * 删除某个角色
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除角色")
    @RequestMapping(value = "/role/{id}",method = RequestMethod.DELETE)
    public ResultBean deleteOneRole(@PathVariable Integer id){
        if (roleService.removeById(id)){
            return ResultBean.ok().message("删除角色操作成功!");
        }
        return ResultBean.error().message("删除角色操作失败，请重试!");
    }

    /**
     * 批量删除角色
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除角色")
    @RequestMapping(value = "/",method = RequestMethod.DELETE)
    public ResultBean deleteRoles(Integer[] ids){
        if (roleService.removeByIds(Arrays.asList(ids))){
            return ResultBean.ok().message("批量删除角色操作成功!");
        }
        return ResultBean.error().message("批量删除角色操作失败，请重试!");
    }

    /**
     * 查询所有菜单列表
     *
     * @return
     */
    @ApiOperation(value = "查询所有菜单")
    @RequestMapping(value = "/menus",method = RequestMethod.GET)
    public ResultBean getAllMenus(){
        List<MenusVO> allMenus = menuService.getAllMenus();
        if (null == allMenus){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().data("Menus",allMenus);
    }


    /**
     * 根据角色id查询菜单id
     *      * 通过 QueryWrapper 查出结果
     *      * 通过流运算，转为list<Integer>
     *
     * @return
     */
    @ApiOperation(value = "根据角色id查询菜单id")
    @RequestMapping(value = "/mid/{rid}",method = RequestMethod.GET)
    public List<Integer> getMenusIdsByRoleId( @PathVariable Integer rid){
        return menuRoleService.list(new QueryWrapper<MenuRole>().eq("rid",rid))
                .stream()
                .map(MenuRole::getMid)
                .collect(Collectors.toList());
    }

    /**
     * 更新角色菜单
     *
     * @param rid 菜单id
     * @param mids 角色id
     * @return
     */
    @ApiOperation(value = "更新角色菜单")
    @RequestMapping(value = "/",method = RequestMethod.PUT)
    public ResultBean updateMenuRole(Integer rid, Integer[] mids){
        return menuRoleService.updateMenuRole(rid,mids);
    }

}
